package com.quectel.core.module.really.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.really.dao.ReallyEquipmentDao;
import com.quectel.core.module.really.dto.ReallyEquipmentDto;
import com.quectel.core.module.really.entity.ReallyEquipmentEntity;
import com.quectel.core.module.really.service.ReallyEquipmentService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-09 14:25:54
 */
@DubboService
public class ReallyEquipmentServiceImpl implements ReallyEquipmentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ReallyEquipmentServiceImpl.class);

    private static final String REALLY_EQUIPMENT_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "REALLY_EQUIPMENT_CACHE_BY_ID:";

    @Autowired
    private ReallyEquipmentDao reallyEquipmentDao;
    @Autowired
    private VillageService villageService;

    @Override
    public ReallyEquipmentDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                REALLY_EQUIPMENT_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ReallyEquipmentDto selectById(Long id) {
        ReallyEquipmentEntity entity = reallyEquipmentDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ReallyEquipmentDto.class));
    }

    @Override
    public List<ReallyEquipmentDto> queryList(Map<String, Object> params) {
        List<ReallyEquipmentEntity> list = reallyEquipmentDao.queryList(params);
        List<ReallyEquipmentDto> result = CopyUtils.copyList(list, ReallyEquipmentDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private ReallyEquipmentDto paddingField(ReallyEquipmentDto dto) {
        if (dto != null) {
            if (dto.getVillageId() != null) {
                VillageDto villageDto = villageService.selectById(dto.getVillageId());
                if (villageDto != null) {
                    dto.setVillageName(villageDto.getName());
                }
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return reallyEquipmentDao.queryTotal(params);
    }

    @Override
    public Long save(ReallyEquipmentDto dto) {
        ReallyEquipmentEntity entity = CopyUtils.copyObj(dto, ReallyEquipmentEntity.class);
        entity.setId(Snowflake.nextId());
        reallyEquipmentDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ReallyEquipmentDto dto) {
        ReallyEquipmentEntity entity = CopyUtils.copyObj(dto, ReallyEquipmentEntity.class);
        reallyEquipmentDao.updateById(entity);
        CacheKit.invalidRedisCache(REALLY_EQUIPMENT_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(ReallyEquipmentDto dto) {

        ReallyEquipmentEntity entity = CopyUtils.copyObj(dto, ReallyEquipmentEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, reallyEquipmentDao);

        CacheKit.invalidRedisCache(REALLY_EQUIPMENT_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        reallyEquipmentDao.deleteById(id);
        CacheKit.invalidRedisCache(REALLY_EQUIPMENT_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
